package com.qk.management.controller;

import com.qk.common.Result;
import com.qk.common.util.AliYunOSSOperators;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Objects;
import java.util.UUID;

/**
 * 文件处理控制器
 *
 * @Author:Mr.m
 * @Date: 2025-10-08 15:36
 * @Description: TODO
 */
@Slf4j
@RestController
public class FileHandleController {
    @Autowired
    private AliYunOSSOperators oss;
    @PostMapping("/upload")
    public Result upload(@RequestParam("image")MultipartFile image) {
        log.info("上传文件：{}",image);
        try{
            //参数校验
            Objects.requireNonNull(image,"文件不能为空");
            Objects.requireNonNull(image.getOriginalFilename(),"文件名不能为空");
            //文件名
            String objectName = UUID.randomUUID()+image.getOriginalFilename().substring(image.getOriginalFilename().lastIndexOf("."));
            //文件上传返回url
            String url = oss.upload(image.getBytes(),objectName);
            return Result.success(url);
        }
        catch (Exception e) {
            log.error("上传文件失败：{}",e.getMessage());
            return Result.error("上传文件失败");
        }
    }


//    @PostMapping("/upload")
//    public String upload(String username , Integer age , MultipartFile  file) throws IOException {
//        //log.info("用户名:{},年龄：{},上传文件：{}",username,age,file);
//        log.info("文件的原始名称：{}",file.getOriginalFilename());
//        log.info("文件类型：{}",file.getContentType());
//        log.info("文件的大小：{}",file.getSize());
//        log.info("获取文件表单项目提交name属性值:{}",file.getName());
//        log.info("文件是否为空：{}",file.isEmpty());
//        log.info("文件保存的路径：{}",file.getResource());
//        file.transferTo(new File("D:/"+file.getOriginalFilename()));
//        return "上传成功";
//    }

}
